package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.common.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;


/**
 * @author : Ar1es
 * @date : 2020/1/19
 * @since : Java 8
 */
@RestController
@Slf4j
public class PreviewController {



    @RequestMapping(value = "/preview/**", method = RequestMethod.GET)
    public void preview(HttpServletRequest request, HttpServletResponse response) {
        String[] fileName = request.getRequestURL().toString().split("preview/");
        log.info("进来了  " + fileName[1]);
        File file = new File(fileName[1]);
        if (file.exists()) {
            byte[] data = null;
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
                data = new byte[inputStream.available()];
                inputStream.read(data);
                response.getOutputStream().write(data);
            } catch (Exception e) {
                System.out.println("文件处理异常");
                e.printStackTrace();
            } finally {
                try{
                    if (null != inputStream) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
